/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.nai365.nart.admin.handler;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nai365.nart.admin.api.dto.UserDTO;
import com.nai365.nart.admin.api.dto.UserInfo;
import com.nai365.nart.admin.api.entity.SysUser;
import com.nai365.nart.admin.feign.RemotePersonService;
import com.nai365.nart.admin.service.SysUserService;
import com.nai365.nart.common.core.constant.SecurityConstants;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lengleng
 * @date 2018/11/18
 */
@Slf4j
@Component("PHO")
@AllArgsConstructor
public class PhoneLoginHandler extends AbstractLoginHandler {
	private final SysUserService sysUserService;
	RemotePersonService remotePersonService;


	/**
	 * 验证码登录传入为手机号
	 * 不用不处理
	 *
	 * @param phone
	 * @return
	 */
	@Override
	public String identify(String phone) {
		return phone;
	}

	/**
	 * 通过mobile 获取用户信息
	 *
	 * @param identify
	 * @return
	 */
	@Override
	public UserInfo info(String identify) {
		String[] resut = null;
		Long inviteId = 0L;
		if (identify.contains(":")) {
			resut = identify.split(":",-1);
			identify = resut[0];
			if (StrUtil.isNotBlank(resut[1])) {
				inviteId = Long.parseLong(resut[1]);
			}
		}
		SysUser sysUser = getOne(identify);
		if (sysUser!=null && "1".equals(sysUser.getDelFlag())){
			throw new RuntimeException("账号已被注销");
		}
		if (sysUser == null) {
			log.info("手机号未注册:{}", identify);
			int length = identify.length();
			List role = new ArrayList();
			role.add(2L);
			UserDTO user1 = new UserDTO();
			user1.setUsername(identify);
//			user1.setPassword(identify.substring(length - 6 ,length));
			user1.setPhone(identify);
			user1.setDeptId(10L);
			user1.setRole(role);
			user1.setLockFlag("0");
			Boolean saveUser = sysUserService.saveUser(user1);
			sysUser = getOne(identify);

			remotePersonService.saveInformation(sysUser.getUserId() , identify , inviteId, SecurityConstants.FROM_IN);
		}
		return sysUserService.findUserInfo(sysUser);
	}

	@Override
	public Boolean bind(SysUser user, String identify) {
		return null;
	}

	public SysUser getOne(String identify){
		SysUser user = sysUserService
				.getOne(Wrappers.<SysUser>query()
						.lambda().eq(SysUser::getPhone, identify).or().eq(SysUser::getPhone, identify).eq(SysUser::getDelFlag,1));
		return user;
	}
}
